﻿@{
    WebSecurity.RequireAuthenticatedUser();
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "New & Edit Reference Data";
    
    var db = Database.Open("CCMS");
    var queryRefType = db.Query("select RefTypeCode, RefTypeDesc from ReferenceType union select '' as RefTypeCode, '' as RefTypeDesc order by RefTypeDesc");
    var serviceid = UrlData[1];
    var refType= Page.RefType;
    var refId= Page.RefID;
    var refDesc= Page.RefDesc;
    var queryparentid = RefDataManager.GetParentRefId("");
}

<style type="text/css">
* { font-family: Verdana; font-size: 96%; }
label { width: 15em; float: left; }
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
p { clear: both; }
.submit { margin-left: 12em; }
em { font-weight: bold; padding-right: 1em; vertical-align: top; }
</style>
<script>
  //  $(function(){
      //  editCC(data);
    //});
    
    //from Customerform
     $(function(){
     
        GetParentID('newptype','newpid','newpid');  
        $("#buttonNew").hide();
        
        if(@Page.Mode=='1'){
           openRefDataForm();
       
                $("#save").button().click(function(){
                 EditRefData();
                 });          
        
        }
        
        
        if(@Page.Mode=='2'){
          $("#refdiv").show();
            $("#save").button().click(function(){
                 saveRefData();
                var ref1 = $("#newrefid").val();
                var ref2 = $("#newreftype").val();
                var ref3 = $("#newdesc").val();
                var ref4 = $("#active").val();
        
                 loadCCList(ref1, ref2, ref3, ref4); 
                     $("#buttonNew").show();
                 });     
        }
        
        $("#cancel").click(function(){
                 window.location.href = "/Maintenance/RefData";
                 });
    

   }); 

 function loadCCList(param1,param2,param3,param4){
     var dummy = new Date().getTime();
    $("#grid").empty();
    $.post("/Maintenance/AddDataGrid?RefId="+ param1 +"&RefType="+param2+"&RefDesc="+param3+"&Active="+param4,function(data){
        $("#grid").html(data); 
    });
    }

function saveRefData(){
     var data = $("#ref_data_form").serialize();
    $.post("/Maintenance/Services/RefDataService/add",data,function(data){
            if(data[2]==null){
            var ccData = data[0];
        
           
            $("#message").empty();
            $("#newptype").val(ccData.ParentRefType);
            $("#newpid").val(ccData.ParentRefID);
            $("#newreftype").val(ccData.RefType);
            $("#newrefid").val(ccData.RefID);
            $("#active").val(ccData.Active);
            $("#newdesc").val(ccData.RefDesc);
            $("#ctcbcode").val(ccData.CTCBCode);
            $("#ctcbdesc").val(ccData.CTCBDesc);
            $("#ubphcode").val(ccData.UBPHCode);
            $("#ubphdesc").val(ccData.UBPHDesc);
            $("#pb3code").val(ccData.PB3Code);
            $("#pb3desc").val(ccData.PB3Desc);
            $("#pb4code").val(ccData.PB4Code);
            $("#pb4desc").val(ccData.PB4Desc);
            $("#pb5code").val(ccData.PB5Code);
            $("#pb5desc").val(ccData.PB5Desc);
            $("#pb6code").val(ccData.PB6Code);
            $("#pb6desc").val(ccData.PB6Desc);
            $("#pb7code").val(ccData.PB7Code);
            $("#pb7desc").val(ccData.PB7Desc);
            $("#pb8code").val(ccData.PB8Code);
            $("#pb8desc").val(ccData.PB8Desc);
        
           // clearForm("#ccForm");
            //dialogCCSaveMode(createCashCard);
                 $("#message").html("<p class='message error'>Reference Data has been saved</p>");
                 $("#refdiv").dialog("close");
                 
                }
                else{
                    $("#message").html("<p class='message error'>Reference Data may have duplicates</p>");
                }
    },"json");
}

function EditRefData(){
     var data = $("#ref_data_form").serialize();
    
    $.post("/Maintenance/Services/RefDataService/esave",data,function(data){
           if(data[2]==null){
        var ccData = data[1];
        
            $("#newptype").val(ccData.ParentRefType);
            $("#newpid").val(ccData.ParentRefID);
            $("#newreftype").val(ccData.RefType);
            $("#newrefid").val(ccData.RefID);
            $("#active").val(ccData.Active);
            $("#newdesc").val(ccData.RefDesc);
            $("#ctcbcode").val(ccData.CTCBCode);
            $("#ctcbdesc").val(ccData.CTCBDesc);
            $("#ubphcode").val(ccData.UBPHCode);
            $("#ubphdesc").val(ccData.UBPHDesc);
            $("#pb3code").val(ccData.PB3Code);
            $("#pb3desc").val(ccData.PB3Desc);
            $("#pb4code").val(ccData.PB4Code);
            $("#pb4desc").val(ccData.PB4Desc);
            $("#pb5code").val(ccData.PB5Code);
            $("#pb5desc").val(ccData.PB5Desc);
            $("#pb6code").val(ccData.PB6Code);
            $("#pb6desc").val(ccData.PB6Desc);
            $("#pb7code").val(ccData.PB7Code);
            $("#pb7desc").val(ccData.PB7Desc);
            $("#pb8code").val(ccData.PB8Code);
            $("#pb8desc").val(ccData.PB8Desc);
        
           // clearForm("#ccForm");
            //dialogCCSaveMode(createCashCard);
                // $("#refdiv").dialog("close");
                 $("#message").html("<p class='message error'>Reference Data has been updated</p>");
                 }
                else{
                    $("#message").html("<p class='message error'>Reference Data may have duplicates</p>");
                }
    },"json");
}

    function openRefDataForm(){
    
    var param = $("#ref_data_form").serialize();
    //alert(param);
    $.post("/Maintenance/Services/RefDataService/edit",param,
        function(data){
        
        var ccData = data[0];
        
            $("#newptype").val(ccData.ParentRefType);
            
            
            GetSelectedParentID('newptype','newpid','newpid',ccData.ParentRefID);
            $("#newptype").val(ccData.ParentRefType);
            $("#newpid").val(ccData.ParentRefID);
            $("#newreftype").val(ccData.RefType);
            $("#newrefid").val(ccData.RefID);
            $("#active").val(ccData.Active);
            $("#newdesc").val(ccData.RefDesc);
            $("#ctcbcode").val(ccData.CTCBCode);
            $("#ctcbdesc").val(ccData.CTCBDesc);
            $("#ubphcode").val(ccData.UBPHCode);
            $("#ubphdesc").val(ccData.UBPHDesc);
            $("#pb3code").val(ccData.PB3Code);
            $("#pb3desc").val(ccData.PB3Desc);
            $("#pb4code").val(ccData.PB4Code);
            $("#pb4desc").val(ccData.PB4Desc);
            $("#pb5code").val(ccData.PB5Code);
            $("#pb5desc").val(ccData.PB5Desc);
            $("#pb6code").val(ccData.PB6Code);
            $("#pb6desc").val(ccData.PB6Desc);
            $("#pb7code").val(ccData.PB7Code);
            $("#pb7desc").val(ccData.PB7Desc);
            $("#pb8code").val(ccData.PB8Code);
            $("#pb8desc").val(ccData.PB8Desc);
        
           // clearForm("#ccForm");
            //dialogCCSaveMode(createCashCard);
             //    $("#refdiv").dialog("open");
    },"json");

    }
    
</script>
    
    <form id="ref_data_form">
<fieldset class="ui-widget ui-widget-content">
<div id="refdiv"  method="post"  class="ui-widget" >
        
        <div id="message" ></div>
        <input type="hidden" name="refType" id="refType" value="@refType"/>
        <input type="hidden" name="refId" id="refId" value="@refId"/>
        <input type="hidden" name="refDesc" id="refDesc" value="@refDesc"/>
            
        <p><label for="newptype">PARENT REFERENCE TYPE </label><select name="newptype" id="newptype">
                @foreach(var ptype in queryRefType){
                    <option value="@ptype.RefTypeCode">@ptype.RefTypeDesc</option>
                }
        </select></p>
        <p>
        <label for="newpid">PARENT REFERENCE ID </label><select name="newpid" id="newpid">
            <option value="">--Select Value--</option>
        </select></p>
        <p><label for="newreftype">REFERENCE TYPE </label><select name="newreftype" id="newreftype">
                @foreach(var reftype in queryRefType){
                    <option value="@reftype.RefTypeCode">@reftype.RefTypeDesc</option>
                }
        </select>
        </p>
        <p><label for="newrefid">REFERENCE ID </label><input type="text" name="newrefid" id="newrefid"/></p>
        <p><label for="active">
        ACTIVE (Y/N) </label><select name="active" id="active"><option value="1">Y</option>
        <option value="0">N</option></select>
        </p>
        <p><label for="newdesc">DESCRIPTION </label><input type="text" name="newdesc" id="newdesc"/></p>
        <p><label for="ctcbcode">CTCB CODE </label><input type="text" name="ctcbcode" id="ctcbcode" /></p>
        <p><label for="ctcbdesc">CTCB DESC </label><input type="text" name="ctcbdesc" id="ctcbdesc" /></p>
        <p><label for="ubphcode">UBPH CODE </label><input type="text" name="ubphcode" id="ubphcode" /></p>
        <p><label for="ubphdesc">UBPH DESC </label><input type="text" name="ubphdesc" id="ubphdesc" /></p>
        <p><label for="pb3code">PB 3 CODE </label><input type="text" name="pb3code" id="pb3code" /></p>
        <p><label for="pb3desc">PB 3 DESC </label><input type="text" name="pb3desc" id="pb3desc" /></p>
        <p><label for="pb4code">PB 4 CODE </label><input type="text" name="pb4code" id="pb4code" /></p>
        <p><label for="pb4desc">PB 4 DESC </label><input type="text" name="pb4desc" id="pb4desc" /></p>
        <p><label for="pb5code">PB 5 CODE </label><input type="text" name="pb5code" id="pb5code" /></p>
        <p><label for="pb5desc">PB 5 DESC </label><input type="text" name="pb5desc" id="pb5desc" /></p>
        <p><label for="pb6code">PB 6 CODE </label><input type="text" name="pb6code" id="pb6code" /></p>
        <p><label for="pb6desc">PB 6 DESC </label><input type="text" name="pb6desc" id="pb6desc" /></p>
        <p><label for="pb7code">PB 7 CODE </label><input type="text" name="pb7code" id="pb7code" /></p>
        <p><label for="pb7desc">PB 7 DESC </label><input type="text" name="pb7desc" id="pb7desc" /></p>
        <p><label for="pb8code">PB 8 CODE </label><input type="text" name="pb8code" id="pb8code" /></p>
        <p><label for="pb8desc">PB 8 DESC </label><input type="text" name="pb8desc" id="pb8desc" /></p>

        <div id="buttons">
        <input type="button" id="save" value="save"/>
        <input type="button" id="cancel" value="cancel"/>
    </div>
</div>
    </fieldset>
        <div id="grid">
                
    </div>
    <p id="buttonNew"><input type="button" id="button2" value="New"/></p>
</form>